package com.it.common.domain;

import com.it.common.domain.dos.UserDO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginUserDetails implements UserDetails {

    private UserDO userDo;

    private List<String> roleNames;

    private List<String> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

        List<GrantedAuthority> authorities = new ArrayList<>();

        if(!CollectionUtils.isEmpty(roleNames)){
            for(String roleName : roleNames){
                roleName = "ROLE_"+roleName;
                authorities.add(new SimpleGrantedAuthority(roleName));
            }
        }
        if(!CollectionUtils.isEmpty(permissions)){
            for(String permission : permissions){
                authorities.add(new SimpleGrantedAuthority(permission));
            }
        }

        return authorities;
    }

    @Override
    public String getPassword() {
        return userDo.getPassword();
    }

    @Override
    public String getUsername() {
        return userDo.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
